#include "PConvertor.h"


namespace PComunicator
{



PConvertor::PConvertor(void)
{}

PConvertor::~PConvertor(void)
{}

/*
std::vector<byte> PConvertor::FromInt(int theValue)
{
	byte aArray[sizeof(theValue)];
	std::memcpy(aArray,&theValue,sizeof(theValue));

	std::vector<byte> aResult(aArray,aArray+sizeof(theValue));
	return aResult;
}

int PConvertor::ToInt(std::vector<byte> theValue)
{
	int aResult = 0;
	std::memcpy(&aResult,&theValue[0],theValue.size());

	return aResult;
}


std::vector<byte> PConvertor::FromLong(long theValue)
{
	byte aArray[sizeof(theValue)];
	std::memcpy(aArray,&theValue,sizeof(theValue));

	std::vector<byte> aResult(aArray,aArray+sizeof(theValue));
	return aResult;
}

long PConvertor::ToLong(std::vector<byte> theValue)
{
	long aResult = 0;
	std::memcpy(&aResult,&theValue[0],theValue.size());

	return aResult;
}
*/

std::vector<byte> PConvertor::FromString(std::string theValue)
{
	std::vector<byte> aResult;
	for(unsigned int i =0; i < theValue.size(); i++)
	{
		aResult.push_back(static_cast<byte>(theValue[i]));
	}

	return aResult;
}

std::string PConvertor::ToString(std::vector<byte> theValue)
{
	std::string aResult;
	for(unsigned int i =0; i < theValue.size(); i++)
	{
		aResult.append(std::string(1,static_cast<char>(theValue[i])));
	}
	return aResult;
}


std::vector<byte> PConvertor::FromString(std::wstring theValue)
{
	std::vector<byte> aResult;
	std::vector<byte> aWideChar;

	for(unsigned int i=0; i < theValue.size(); i++)
	{
		aWideChar = FromBasicType<int>((static_cast<int>(theValue[i])));
		std::copy(aWideChar.begin(),aWideChar.end(),back_inserter(aResult));
	}
	return aResult;
}

std::wstring PConvertor::ToWString(std::vector<byte> theValue)
{
	std::wstring aResult;
	std::vector<byte> aHolder;
	std::vector<byte>::iterator iterStart = theValue.begin();
	std::vector<byte>::iterator iterEnd = theValue.begin();

	iterEnd+=4;
	while(iterEnd != theValue.end())
	{
		aHolder.clear();

		std::copy(iterStart,iterEnd,back_inserter(aHolder));

		aResult.append(std::wstring(1,static_cast<wchar_t>(ToBasicType<int>(aHolder))));
		
		iterStart = iterEnd;
		iterEnd+=4;
	}
	
	aHolder.clear();
	std::copy(iterStart,theValue.end(),back_inserter(aHolder));
	aResult.append(std::wstring(1,static_cast<wchar_t>(ToBasicType<int>(aHolder))));

	return aResult;
}



//******************************************************************//
//						 PRIVATE METHODS							//
//******************************************************************//


void PConvertor::NullArray(char *pArray,int theSize)
{
	for(int i=0; i < theSize; i++)
	{
		pArray[i] = '\0';
	}
}


}